package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsStorage;
import com.itqf.dto.DtsSearchDto;
import com.itqf.service.DtsStorageService;
import com.itqf.util.ResponseUtil;
import com.itqf.util.UUID;
import com.itqf.utils.AliyunOSSUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/16 16:59
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("admin/storage")
public class AdminStorageController {


    /**
     * oss服务器地址
     * oss.endpoint=http://oss-cn-beijing.aliyuncs.com
     * oss.accessKeyId=LTAI5t82cN3TfofxJywKvoj5
     * oss.accessKeySecret=7cPhJQBMNewuqXXnUiEjpj1l8YeONo
     * oss.bucketName=bk-java
     */
    @Value("${oss.endpoint}")
    private String endpoint;
    /**
     * 用户访问id
     */
    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    /**
     * 用户访问密码
     */
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    /**
     * 桶的名字
     */
    @Value("${oss.bucketName}")
    private String bucketName;



    @Autowired
    private DtsStorageService dtsStorageService;

    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu={"系统管理","对象管理"},button="对象添加")
    @PostMapping("create")
    public Object create(MultipartFile file) throws Exception {

        System.out.println("-----"+endpoint+"-----");

        /**
         *     * @param endpoint          OSS对外服务的访问域名
         *      * @param accessKeyId       accessKey账号
         *      * @param accessKeySecret   accessKey密码
         *      * @param bucketName        桶名字
         *      * @param objectName        完整文件名, 例如abc/efg/123.jpg
         *      * @param content           文件内容, byte数组格式
         *
         */

        String fileName = file.getOriginalFilename();

        String key = UUID.randomUUID().toString().replaceAll("-", "")+fileName;

        String url = AliyunOSSUtil.uploadImage(endpoint,accessKeyId,accessKeySecret,bucketName,key,file.getBytes());


        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setKey(key);
        dtsStorage.setName(fileName);
        dtsStorage.setType(fileName.substring(fileName.lastIndexOf(".")));
        dtsStorage.setSize((int) file.getSize());
        dtsStorage.setUrl(url);
        dtsStorage.setAddTime(new Date());
        dtsStorage.setDeleted(false);

        //1.存到oss里面
        //2.记录保存到数据
        dtsStorageService.save(dtsStorage);

        return ResponseUtil.ok(dtsStorage);
    }



    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu={"系统管理","对象管理"},button="对象列表")
    @GetMapping("list")
    public Object list(DtsSearchDto dto)
    {

        Map data = dtsStorageService.query(dto);

        return ResponseUtil.ok(data);
    }

}
